package main

import (
	"flag"
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"time"
	//        "bytes"
)

func SendSms(smsparams, phone, Mess string) {
	//enc:=mahonia.NewEncoder("gbk")
	//  consgbk:= enc.ConvertString(cons)

	flag.Parse()

	postValue := url.Values{}
	postValue.Add(*CellPhone, phone)
	postValue.Add(*Content, Mess)

	skey := strings.Split(*Smsparamkey, ",")
	sval := strings.Split(smsparams, ",")
	for i, v := range skey {
		postValue.Add(v, sval[i])
	}
	//	fmt.Println("postValue:", postValue)

	resp, err := http.PostForm(*SmsURL, postValue)

	/* 尝试过另一种方法，代码暂时先放着

	//        SendUrl:=*SmsURL+"?"+*CellPhone+"="+phone
	//       fmt.Println(SendUrl)
	// 	for i, v := range skey {
	// 		SendUrl=SendUrl+"&"+v+"="+sval[i]
	// 	}

	//       SendUrl=SendUrl+"&"+*Content+"="+Mess
	//      fmt.Println(SendUrl)
	//     client := &http.Client{}
	//    msg:="test"
	//   req, _ := http.NewRequest("POST", SendUrl, bytes.NewBuffer([]byte(msg)))
	// 	req.Header.Set("Content-Type", "application/json")
	// 	req.Header.Set("charset", "UTF-8")
	//       resp, err := client.Do(req)
	*/
	if err != nil {
		// handle error
		fmt.Println(err)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[ERROR]往短信服务发送短信报错，请检查网络，以请参数是否正确")
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[INFO]短信URL：", *SmsURL)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[INFO]短信参数：", postValue)
		//		panic(err)
		return
	}
	if *debugMode {
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[DEBUG]SMS debug信息")
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[debug]SMS短信URL：", *SmsURL)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[debug]SMS短信参数：", postValue)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[debug]SMS-resp:", resp)
		//         fmt.Println("[debug]SendUrl:",SendUrl)
	}

	//fmt.Println(resp)
	defer resp.Body.Close()

}
